package stringdemo;

import java.util.Scanner;

/*
针对于收据的金额转换
反向逆推法
查表法
 */
public class StringDemo7StringConversionOfAmount {
    public static void main(String[] args) {
        //1.键盘录入一个金额
        Scanner scan = new Scanner(System.in);
        int money;
        while (true){
            System.out.println("请输入一个金额:");
            money = scan.nextInt();
            //有效数字判断(小于0、大于7重新输入)
            if (money<0 || money>9999999){
                System.out.println("金额无效!");
            }else break;
        }

        //定义一个变量来表示钱的大写
        String moneyStr = "";
        //2.得到money里面的每一个数字,转成中文
        while (true){
            //从右往左获取数据集，因为右侧时数组的个位
            int tmp = money % 10;
            //把转化之后的大写拼接到money当中
            String capitalNumber = getCapitalNumber(tmp);
            moneyStr = capitalNumber + moneyStr;
            //去掉刚刚获取的数据
            money = money / 10;
            //如果数字上的每一位全部获取到了，那么money记录的钱就是0，此时循环结束
            if (money == 0) break;
        }

        //3.在前面补0，补齐7位
        int count = 7 - moneyStr.length();
        for (int i = 0; i < count; i++) {
            moneyStr = "零" + moneyStr;
        }

        //4.插入单位(查表法)
        //定义一个数组来表示单位
        String[] arr = {"佰","拾","万","仟","佰","拾","元"};

        String result = "";
        for (int i = 0; i < moneyStr.length(); i++) {
            //遍历moneyStr,依次得到money
            char tmp = moneyStr.charAt(i);
            //然后把arr的单位插入进去
//            System.out.print(tmp+arr[i]);
            //把大写数组和单位拼接到result当中
            result = result + tmp + arr[i];
        }
        System.out.println(result);


    }


    //定义一个方法把数字变成大写的中文
    //1 -- 壹
    public static String getCapitalNumber(int number){
        //定义数组，让数字跟大写的中文产生一个对应关系
        //零、壹、贰、叁、肆、伍、陆、柒、捌、玖
        String[] arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        return arr[number];
    }
}
